<?php
namespace app\common\model;

use plug\SUtil;

class User extends Base
{
    // 设置当前模型对应的完整数据表名称

    protected $table = 'ddt_user';

    // 主键ID
    protected $pk = 'user_id';

	public function OpenidGetUser($openid,$field = 'user_id,nick_name'){
		return $this->field($field)->where(['wx_openid'=>$openid])->find();
	}

	public function PhoneGetUser($phone,$field = 'user_id,nick_name'){
		return $this->field($field)->where(['phone'=>$phone])->find();
	}


    public function UnionidGetUser($unionid,$field = 'user_id,nick_name'){
//	    $user = $this->field($field)->where(['unionid'=>$unionid])->find();
//        if(!$user){
//            $data = $this->field($field)->where(['wx_openid'=>$openid])->find();
//            if($data){
//                $datas = ['unionid' => $unionid];
//                $data = $data->save($datas);
//            }
//        }
        return $this->field($field)->where(['unionid'=>$unionid])->find();
    }
    /**
     * 注册用户
     * @param $nickname
     * @param $sex
     * @param $headimgurl
     * @param $openid
     * @return void
     */
    public function binduser($nickname,$sex,$headimgurl,$openid,$unionid,$fromuid = '',$field = 'wx_openid'){
        $ip = SUtil::getIP();
        $data = [
            'utype' => 1,
            'nick_name' => $nickname,
            'sex' => $sex,
            'avatar' => $headimgurl,
            'add_ip' => $ip,
            "{$field}" => $openid,
            'unionid' => $unionid,
            'lastlogin' => time(),
            'loginip' => $ip,
            'invite_uid' => $fromuid,
            'add_time' => time(),
        ];
        $res = $this->save($data);
        if(!$res){
            return false;
        }
        return $this->{$this->pk};
    }
	/**
	 * 注册用户
	 * @param $nickname
	 * @param $sex
	 * @param $headimgurl
	 * @param $openid
	 * @return void
	 */
	public function phonebinduser($nickname,$sex,$headimgurl,$phone,$fromuid = ''){
		$ip = SUtil::getIP();
		$data = [
			'utype' => 1,
			'nick_name' => $nickname,
			'sex' => $sex,
			'avatar' => $headimgurl,
			'phone' => $phone,
			'add_ip' => $ip,
			'lastlogin' => time(),
            'add_time' => time(),
			'loginip' => $ip,
            'invite_uid' => $fromuid,
		];
		$res = $this->save($data);
		if(!$res){
			return false;
		}
		return $this->{$this->pk};
	}

	/**
	 * 注册用户
	 * @param $nickname
	 * @param $sex
	 * @param $headimgurl
	 * @param $openid
	 * @return void
	 */
	public function login($userId,$openid,$field = '',$fromUid = ''){
		$ip = SUtil::getIP();
		$data = [
			'lastlogin' => time(),
			'loginip' => $ip,
		];
        if(!empty($field)){
            $data["{$field}"] = $openid;
        }
        if(!empty($fromUid)){
            $data['invite_uid'] = $fromUid;
        }
		$res = $this->save($data,['user_id' => $userId]);
		return $res;
	}

    /**
     * @param array $where  // 查询的条件
     * @param string $item  // 查询的参数
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function getOne($where=[1], $item='*')
    {
        $rs = $this
            ->field($item)
            ->where($where)
            ->find();

        return $rs;
    }

    // 获取列表
    function get_list($item)
    {
        $rs = $this
            ->field($item)
            ->select();
        return $rs;
    }

    // 获取分页列表
    function get_list_page($cond,$orderby,$item,$limit)
    {
        $where = null;
//        if(!empty($cond['shopname'])){
//            $where[] = ['a.shopname','like',"%".$cond['shopname']."%"];
//        }

        if(!empty($cond['utype'])){
            $where[] = ['utype','=', $cond['utype']];
        }

        if(!empty($cond['is_sales'])){
            $where[] = ['is_sales','=', $cond['is_sales']];
        }


        $rs = $this
//            ->alias('a')
//            ->leftJoin('ddt_subplatform b','a.spf_id = b.spf_id')
            ->field($item)
            ->where($where)
            ->order($orderby)
            ->paginate($limit);

        return $rs;
    }


//    用户兑换商品减少积分
    function reducepoints($user,$integral){
        $condition = [
            "user_id" => $user['user_id'],
        ];
        $rs = $this
            ->where($condition)
            ->setDec('growth_coin',$integral);
        return $rs;
    }

}